package com.kkm.controller;

import com.kkm.dao.Imp.GoodsDaoImp;
import com.kkm.pojo.Goods;
import com.kkm.pojo.Order;
import com.kkm.pojo.PageBean;
import com.kkm.service.GoodsService;
import com.kkm.service.Imp.GoodsServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.Map;

@WebServlet("/goods")
public class GoodsServlet extends BaseServlet {
    GoodsService goodsService = new GoodsServiceImp();
    //展示订单

    public void showAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String currentPage=req.getParameter("currentPage");
        if(currentPage==null||"".equals(currentPage)){
            currentPage="1";
        }
        int rows=8;
        // req.removeAttribute("currPageMax");
        PageBean<Goods> pageBeanOrder=goodsService.selectAll(currentPage,rows);
        req.setAttribute("Goods",pageBeanOrder);
        req.getRequestDispatcher("/showAllGoods.jsp").forward(req,resp);
    }
    //模糊查询订单
    public void checkGoods(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        //获取查询条件，封装成集合
        Map<String, String[]> condition = req.getParameterMap();
        for(String key : condition.keySet()){
            System.out.println("商品："+key+" "+condition.get(key)[0]);
        }

        String currentPage=req.getParameter("currentPage");

        int rows=8;
        req.removeAttribute("currPageMax");
        PageBean<Goods> goodsList=goodsService.checkGoods(condition,currentPage,rows);
        if(goodsList==null){
//            userList=service.showUser(currentPage,rows); 如果需要在查询不到时显示全部数据
            req.setAttribute("Goods",goodsList);
            req.getRequestDispatcher("/showAllGoods.jsp").forward(req,resp);
        }else {
            req.setAttribute("Goods",goodsList);
            req.getRequestDispatcher("/showAllGoods.jsp").forward(req,resp);
        }

    }
    //删除订单
    public void deleteGoods(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int gid = Integer.parseInt(req.getParameter("gid"));
        goodsService.deleteGoods(gid);

        String currentPage=req.getParameter("currentPage");
        int rows=8;
        PageBean<Goods> pageBeanOrder=goodsService.selectAll(currentPage,rows);
        req.setAttribute("Goods",pageBeanOrder);
        req.getRequestDispatcher("/showAllGoods.jsp").forward(req,resp);
    }
//    //修改用户

    //修改用户（第一步接收信息）
    public void goodsById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int gid= Integer.parseInt(req.getParameter("gid"));
        Goods goods=goodsService.goodsById(gid);
        req.setAttribute("goods",goods);
        req.getRequestDispatcher("/modifyGoods.jsp").forward(req,resp);
    }
    public void modifyGoods(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {

        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        int oid= Integer.parseInt(req.getParameter("gid"));
        double price= Double.parseDouble(req.getParameter("price"));

        int number = Integer.parseInt(req.getParameter("number"));


        goodsService.modifyGoods( price,number,oid);

        String currentPage=req.getParameter("currentPage");
        int rows=8;
        PageBean<Goods> pageBeanUser=goodsService.selectAll(currentPage,rows);
        req.setAttribute("Goods",pageBeanUser);
        req.getRequestDispatcher("/showAllGoods.jsp").forward(req,resp);
    }
    public void addGoods(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {

        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        String gname = req.getParameter("gname");

        double price = Double.parseDouble(req.getParameter("price"));
        int number = Integer.parseInt(req.getParameter("number"));
        String gtype = req.getParameter("gtype");
        System.out.println(gname+" "+price+" "+number+" "+gtype);

        goodsService.addGoods(gname,price,number,gtype);

        String currentPage=req.getParameter("currentPage");
        int rows=8;
        PageBean<Goods> pageBeanUser=goodsService.selectAll(currentPage,rows);
        req.setAttribute("Goods",pageBeanUser);
        req.getRequestDispatcher("/showAllGoods.jsp").forward(req,resp);
    }

}
